home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / doc / classes.man < prev    next >
Text File  |  1992-08-03  |  13KB  |  387 lines

  1. .SH CLASS DEFINITIONS
  2. .ds CI \f4CmdLineArgIter\fP
  3. .ds CA \f4CmdArg\fP
  4. .ds CL \f4CmdLine\fP
  5. .de Ss
  6. .br
  7. \fB\s-1\&\\$1:\s+1\fR
  8. .br
  9. ..
  10. .de UL
  11. .if n \\$1
  12. .if n .br
  13. ..
  14. .de CS
  15. .  ft 4
  16. .  nf
  17. ..
  18. .de CE
  19. .  fi
  20. .  ft R
  21. ..
  22. .PP
  23. For the most "up to date" explanation of each class (and of its members),
  24. please refer to the relevant include files (which are thoroughly commented)
  25. mentioned in the \s-1\fBFILES\fP\s+1 section. The most common facilities
  26. of the most commonly used classes are described in the following subsections.
  27. .\"-----------------------------------------------
  28. .SS "\s+1Class \*(CI\s-1"
  29. .RS
  30. Class \*(CI is an abstract base class for iterating over string
  31. arguments from the command-line (or from some other input source).
  32. It has two member functions (both of which are pure virtual):
  33.  
  34. .IP "\f4const char * operator()(void);\fP"
  35. This member function returns the current string argument and then
  36. advances to the next string argument. If no arguments are left then
  37. \f4NULL\fP is returned.
  38.  
  39. .IP "\f4int  is_temporary(void)  const;\fP"
  40. This member function is used to indicate the "type" of storage that is
  41. used for the values returned by \f4operator()\fP. Some iterators will
  42. have \f4operator()\fP return values that will stay around at least as
  43. long as the iterator itself; if this is the case, then this member
  44. function will return 0. Other iterators may reuse the same storage
  45. on successive calls to \f4operator()\fP; if this is the case, this
  46. this member function will return a non-zero value.
  47.  
  48. .PP
  49. There are three predefined subclasses of \*(CI: \f4CmdArgvIter\fP,
  50. \f4CmdStrtokIter\fP, and \f4CmdIstreamIter\fP.
  51.  
  52. .CS
  53. class CmdArgvIter : public CmdLineArgIter {
  54. public:
  55.    CmdArgvIter(const char * const argv[]);
  56.  
  57.    CmdArgvIter(int argc, const char * const argv[]);
  58.  
  59.       // Restart using a different string array.
  60.    void  reset(const char * const argv[]);
  61.    void  reset(int argc, const char * const argv[]);
  62.  
  63.    const char * operator()(void);
  64.  
  65.    int  is_temporary(void)  const;
  66. } ;
  67. .CE
  68.  
  69. This class iterates over string arguments that originate from a NULL
  70. terminated vector of strings (such as \f4argv\fP), and may be  constructed
  71. with just an array, or with an array and an item count.
  72.  
  73. .CS
  74. class  CmdStrTokIter : public CmdLineArgIter {
  75. public:
  76.    CmdStrTokIter(const char * tokens, const char * delimiters =NULL);
  77.  
  78.       // Reset using a new token-string and delimiter set.
  79.    void  reset(const char * tokens, const char * delimiters =NULL);
  80.  
  81.       // Get the current delimiter set
  82.    const char *  delimiters(void)  const;
  83.  
  84.       // Change the current delimiter set
  85.    void  delimiters(const char * new_delimiters);
  86.  
  87.    const char * operator()(void);
  88.  
  89.    int  is_temporary(void)  const;
  90. } ;
  91. .CE
  92.  
  93. This class iterates over string arguments that come from a single string
  94. that contains multiple tokens which are delimited by one or more characters
  95. from the given delimiter set. The \f4strtok(3C)\fP library function is 
  96. used to extract tokens from the string. If a delimiter set of \f4NULL\fP
  97. is given then whitespace will be assumed.
  98.  
  99. .CS
  100. class  CmdIstreamIter : public CmdLineArgIter {
  101. public:
  102.    CmdIstreamIter(istream & input);
  103.  
  104.    const char * operator()(void);
  105.  
  106.    int  is_temporary(void)  const;
  107. } ;
  108. .CE
  109.  
  110. This class iterates over string arguments that come from an input stream.
  111. Each line of the input stream is considered to be a set of white-space
  112. separated tokens. If the the first non-white character on a line is `#'
  113. (`!' for VMS systems) then the line is considered a comment and is ignored.
  114. If a line is more than 1022 characters in length then we treat it as if
  115. it were several lines of length 1022 or less.
  116.  
  117. .RE
  118. .\"-----------------------------------------------
  119. .SS "\s+1Class \*(CA\s-1"
  120. .RS
  121. A \*(CA is an abstract command-line argument.
  122. At this level (being the base class), all a command argument
  123. contains is the "interface" (on the command-line) of the
  124. argument, and some information (after the command-line has
  125. been parsed) that says "how" the argument appeared (if it
  126. was specified).
  127.  
  128. The interface of a \*(CA consists of the following:
  129.  
  130. .RS
  131. .IP "\(bu" 3
  132. a character name
  133. .IP "\(bu" 3
  134. a keyword name
  135. .IP "\(bu" 3
  136. a value name (if the argument takes a value)
  137. .IP "\(bu" 3
  138. an argument description
  139. .IP "\(bu" 3
  140. a set of flags describing the syntax of the argument.
  141. .IP "\(bu" 3
  142. a set of flags to record how (and if) the argument
  143. appeared on the command-line.
  144. .RE
  145.  
  146. When constructing a \*(CA, the most common syntax-flags can be
  147. inferred from the syntax used in the argument description,
  148. and the argument value name.  If the first non-white character
  149. of the argument description is a semicolon (`;'), then the argument
  150. is considered to be "secret" and is NOT printed in usage messages.
  151. When specifying a value name, one may enclose the value name in between
  152. square brackets (`[' and `]') to indicate the value is optional. Also,
  153. one may follow the actual value name with an ellipsis ("\0.\^.\^.")
  154. to indicate that the value corresponds to a LIST of values.
  155.  
  156. Other more esoteric syntax flags may be specified explicitly by using
  157. one or more of the bitmasks of type \f4CmdArg::CmdArgSyntax\fP as
  158. the last argument to the constructor; if these syntax-flags are NOT supplied,
  159. then reasonable defaults will be used.
  160.  
  161. The different types of constructors for a \*(CA are as follows:
  162.  
  163. .CS
  164.    // Create an option that takes a value.
  165.    //
  166.    // The default flags are to assume that the argument is
  167.    // optional and that the value is required.
  168.    //
  169.    // Examples:
  170.    //    //  [\-c number]  or  [\*(--count number]
  171.    //    CmdArg('c', "count", "number",
  172.    //              "specify the # of copies to use);
  173.    //
  174.    //    //  [\-d [level]]  or  [\*(--debug [level]]
  175.    //    CmdArg('d', "debug", "[level]",
  176.    //              "turn on debugging and optionally"
  177.    //              "specify the debug level");
  178.    //
  179.    //    //  [\-l items ...]  or  [\*(--list items ...]
  180.    //    CmdArg('l', "list", "items ...",
  181.    //              "specify a list of items.");
  182.    //
  183. CmdArg(char         optchar,
  184.        const char * keyword,
  185.        const char * value,
  186.        const char * description,
  187.        unsigned     syntax_flags =CmdArg::isOPTVALREQ);
  188.  
  189.    // Create an option that takes no value.
  190.    //
  191.    // The default syntax-flags are to assume that the
  192.    // argument is optional.
  193.    //
  194.    // Example:
  195.    //    //  -m  or  \*(--mode
  196.    //    CmdArg('m', "mode", "turn on this mode");
  197.    //
  198. CmdArg(char         optchar,
  199.        const char * keyword,
  200.        const char * description,
  201.        unsigned     syntax_flags =CmdArg::isOPT);
  202.  
  203.    // Create a positional argument.
  204.    //
  205.    // The default flags are to assume that the argument is
  206.    // positional and that the argument value is required.
  207.    //
  208.    // Examples:
  209.    //    CmdArg("file", "file to read");
  210.    //
  211.    //    CmdArg("[file]", "optional file to read");
  212.    //
  213.    //    CmdArg("file ...", "list of files to read");
  214.    //
  215.    //    CmdArg("[file ...]", "optional list of files to read");
  216.    //
  217. CmdArg(const char * value,
  218.        const char * description,
  219.        unsigned     syntax_flags =CmdArg::isPOSVALREQ);
  220. .CE
  221.  
  222. After a command-argument has been declared, you may wish to ask several
  223. questions regarding how (and if) it was specified. For example: was the
  224. argument given? If it was given, was a value supplied?  These questions
  225. (and others) may answered using the \f4flags()\fP member function:
  226.  
  227. .CS
  228.      unsigned  CmdArg::flags(void)  const;
  229. .CE
  230.  
  231. This member function returns a set of bitmasks of type
  232. \f4CmdArg::CmdArgFlags\fP which are defined in \f4<cmdline.h>\fP.
  233. The most common flags are \f4CmdArg::GIVEN\fP (which is set if the
  234. argument was given) and \f4CmdArg::VALGIVEN\fP (which is set if a
  235. value was supplied for the argument).
  236.  
  237. There are other member functions to return each of the attributes that
  238. a \*(CA was constructed with.  These member functions (and others) are
  239. discussed in \f4<cmdline.h>\fP.
  240.  
  241. Classes that are derived from \*(CA will not have all three of the above 
  242. destructors. Some derived classes (such as \f4CmdArgBool\fP) only
  243. correspond to non-positional arguments that take no value. Other derived
  244. classes (such as \f4CmdArgInt\fP and \f4CmdArgStr\fP) always permit
  245. a value to be given.
  246. Some predefined subclasses of \*(CA which represent the most commonly used
  247. types of command-line arguments may be found in \f4<cmdargs.h>\fP.
  248.  
  249. .RE
  250. .\"-----------------------------------------------
  251. .SS "\s+1Class \*(CL\s-1"
  252. .RS
  253. Class \*(CL is the class that represents a command-line object.
  254. A command-line object is a parsing machine (with machine states),
  255. whose parsing behavior may be configured at run-time by specifying
  256. various flags of type \f4CmdLine::CmdFlags\fP.
  257. A command-line object also contains a command-name and a list of
  258. \*(CA objects that correspond to the various arguments that are
  259. allowed to occur on the command-line.
  260.  
  261. .Ss "Constructing a \*(CL Object"
  262. .UL "------------------------------"
  263. It is not necessary to supply a command-name at construction
  264. time, but one SHOULD be specified before parsing a command-line
  265. or printing a usage message.
  266.  
  267. Similarly, \*(CAs are not required at construction time and may
  268. even be added on the fly. All desired arguments should be added
  269. before any parsing happens and before printing usage.
  270.  
  271. The order in which \*(CAs are added to a \*(CL is important
  272. because for positional parameters, this specifies the order in
  273. which they are expected to appear on the command-line.
  274.  
  275. The constructors for a \*(CL are as follows (those constructors that
  276. take a variable number of parameters must have \f4NULL\fP specified
  277. as the final parameter):
  278.  
  279. .CS
  280.         // construct a command (and optionally specify a name)
  281.      CmdLine(const char * name =NULL);
  282.  
  283.         // construct a command with a name, and arguments
  284.      CmdLine(const char * name, CmdArg * ...);
  285.  
  286.         // construct a command command with arguments, but no name
  287.      CmdLine(CmdArg * cmdarg, CmdArg * ...);
  288. .CE
  289.  
  290. The command name may be set (or queried) after construction by using
  291. the \f4name()\fP member function.
  292.  
  293. Command arguments may be added after construction by using the \f4append()\fP
  294. member function.
  295.  
  296. .Ss "Other Common \*(CL Member Functions"
  297. .UL "--------------------------------------"
  298. The most common requests to make of a \*(CL object is to ask it to parse
  299. its arguments, to query it's status, to print the command usage, and to
  300. print an error message.  These may be accomplished with the following
  301. member functions:
  302.  
  303. .CS
  304.    // Print usage to the given outstream or to
  305.    // the default error outstream.
  306.    //
  307. ostream &  usage(ostream & os)  const;
  308. ostream &  usage(void)  const;
  309.  
  310.    // Print the command-name, followed by ": " and return the
  311.    // outstream for the user to supply the remainder of the
  312.    // error message.
  313.    //
  314.    // Example:
  315.    //   cmd.error() << "can't use \-x with \-y." << endl ;
  316.    //
  317. ostream &  error(void)  const;
  318.  
  319.    // Obtain the current status of the command. The status will be
  320.    // zero if everything is okay; otherwise it will correspond
  321.    // to a combination of CmdLine::CmdStatus bitmasks telling us
  322.    // precisely what went wrong.
  323.    //
  324. unsigned  status(void)  const;
  325.  
  326.    // Parse arguments from the given string argument iterator.
  327.    // The return value will be the resultant CmdLine status
  328.    // (which may also be obtained using status()).
  329.    //
  330. unsigned  parse(CmdLineArgIter & arg_iter);
  331. .CE
  332.  
  333.  
  334. .Ss "Modifying Parsing Behavior"
  335. .UL "-----------------------------"
  336. If you wish to modify parsing behavior to something other than the default,
  337. there are four more member functions that you will need to know how to use.
  338. Each of these member functions deals with bitmasks of the type
  339. \f4CmdLine::CmdFlags\fP.
  340.  
  341. .CS
  342.    // Flags that define parsing behavior
  343.    //   The default flags (for Unix) are OPTS_FIRST.
  344. enum CmdFlags {
  345.    ANY_CASE_OPTS = 0x001, // Ignore character-case for short-options
  346. .sp 2p
  347.    PROMPT_USER   = 0x002, // Prompt the user for missing required args
  348. .sp 2p
  349.    NO_ABORT      = 0x004, // Don't exit upon syntax error
  350. .sp 2p
  351.    OPTS_FIRST    = 0x008, // No options after positional parameters
  352. .sp 2p
  353.    OPTS_ONLY     = 0x010, // Don't accept short-options
  354. .sp 2p
  355.    KWDS_ONLY     = 0x020, // Don't accept long-options
  356. .sp 2p
  357.    TEMP          = 0x040, // Assume all arg-strings are temporary
  358. .sp 2p
  359.    QUIET         = 0x080, // Don't print syntax error messages
  360. .sp 2p
  361.    NO_GUESSING   = 0x100, // Don't guess if cant match an option. 
  362.                              // Unless this flag is given, then
  363.                              // when we see an unmatched option,
  364.                              // we will try to see if it matches
  365.                              // a keyword (and vice-versa).
  366. } ;
  367.  
  368.    // Get the current set of command-flags
  369. unsigned  flags(void)  const;
  370.  
  371.    // Specify a new set of command-flags
  372. void  flags(unsigned newflags);
  373.  
  374.    // Set only the given command-flags, leave the others alone
  375. void  set(unsigned flags);
  376.  
  377.    // Clear only the given command-flags, leave the others alone
  378. void  clear(unsigned flags =~0);
  379. .CE
  380.  
  381. These are the basic member functions of a \*(CL object. The are others as well
  382. but these should provide most of the desired functionality. For more detailed
  383. information, please see \f4<cmdline.h>\fP.
  384.  
  385. .RE
  386. .\"-----------------------------------------------
  387.